package com.weishu.domain;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author huizhenfei
 * @Description //TODO qq231
 * @Date 20:53 2021/5/18
 * @Param
 * @return
 **/
public class R extends HashMap<String,Object> {
    private static final long serialVersionUID = 1L;

    public R() {
        this.put("code", RC.OK.code);
        this.put("msg", RC.OK.msg);
    }

    public static R of(RC rc, String msg) {
        R r = new R();
        r.put("code", rc.code);
        r.put("msg", msg);
        return r;
    }

    public static R error() {
        return error(RC.INTERNAL_ERROR, "未知异常，请联系管理员");
    }

    public static R error(RC rc) {
        return error(rc.code, rc.msg);
    }

    public static R error(RC rc, String msg) {
        return error(rc.code, msg);
    }

    public static R error(String msg) {
        return error(RC.INTERNAL_ERROR, msg);
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.put("msg", msg);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }

    public static R ok() {
        return new R();
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public enum RC {
        OK(0, "处理成功"),
        NOT_LOGIN(1, "请登录"),
        VALIDATE_ERROR(2, "校验错误"),
        UNAUTHORIZED(401, "操作未被授权"),
        INTERNAL_ERROR(500, "系统繁忙，请稍后重试")
        ;

        int code;
        String msg;
        RC(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }
    }
}
